<template>
  <el-container style="height: 100vh">
    <el-aside :width="asideWidth" class="aside">
      <div class="avatar_box">
        <img :src="logoSrc" alt="logo" class="avatar" />
        <span v-show="!isCollapse" class="system_name">海报设计系统</span>
      </div>
      <el-menu
        :default-active="activeMenu"
        class="el-menu-vertical-demo"
        @select="handleMenuSelect"
        :collapse="isCollapse"
      >
        <el-menu-item index="home">
          <i class="el-icon-menu"></i>
          <span slot="title">首页</span>
        </el-menu-item>
        <el-submenu index="system">
          <template slot="title">
            <i class="el-icon-setting"></i>
            <span>系统管理</span>
          </template>
          <el-menu-item index="user">用户管理</el-menu-item>
          <el-menu-item index="menu">菜单管理</el-menu-item>
          <el-menu-item index="role">角色管理</el-menu-item>
        </el-submenu>
        <el-menu-item index="poster">
          <i class="el-icon-picture"></i>
          <span slot="title">海报管理</span>
        </el-menu-item>
        <el-menu-item index="material">
          <i class="el-icon-document"></i>
          <span slot="title">素材管理</span>
        </el-menu-item>
      </el-menu>
    </el-aside>
    <el-container>
      <el-header class="header">
        <div class="header-left">
          <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'" @click="toggleAside"></i>
          <el-breadcrumb class="breadcrumb" separator="/">
            <el-breadcrumb-item to="/" key="home" @click.native.prevent="handleBreadcrumbClick('home')">首页</el-breadcrumb-item>
            <el-breadcrumb-item v-for="(item, index) in breadcrumbItems" :key="index" :to="item.to" @click.native.prevent="handleBreadcrumbClick(item.name)">
              {{ item.label }}
            </el-breadcrumb-item>
          </el-breadcrumb>
        </div>
        <div class="header-right">
          <el-avatar :src="avatarSrc" size="medium"></el-avatar>
          <el-dropdown>
            <span class="el-dropdown-link">
              admin<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>个人中心</el-dropdown-item>
              <el-dropdown-item>退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
      </el-header>
      <el-main class="main">
        <el-tabs v-model="tabActiveName" type="card" @tab-click="handleTabClick" @tab-remove="removeTab">
          <el-tab-pane v-for="(tab, index) in tabs" :key="tab.name" :label="tab.label" :name="tab.name" :closable="tab.name !== 'home'">
            Tab Content {{ tab.label }}
          </el-tab-pane>
        </el-tabs>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      logoSrc: require("../assets/logo.jpg"), // 引用 logo 图像
      avatarSrc: require("../assets/tx.jpg"), // 引用头像图像
      breadcrumbItems: [], // 初始化面包屑路径
      isCollapse: false,
      asideWidth: '200px', // 初始侧边栏宽度
      tabs: [{ label: '首页', name: 'home' }], // 默认添加首页标签页
      tabActiveName: 'home', // 默认激活首页标签页
      activeMenu: 'home', // 默认激活侧边栏的首页菜单项
    };
  },
  methods: {
    handleMenuSelect(index) {
      // 菜单项的映射，用于显示正确的标签页名称
      const menuMap = {
        'home': '首页',
        'user': '用户管理',
        'menu': '菜单管理',
        'role': '角色管理',
        'poster': '海报管理',
        'material': '素材管理',
      };

      const breadcrumbMap = {
        'home': [],
        'user': [{ label: '系统管理', name: 'system' }, { label: '用户管理', name: 'user' }],
        'menu': [{ label: '系统管理', name: 'system' }, { label: '菜单管理', name: 'menu' }],
        'role': [{ label: '系统管理', name: 'system' }, { label: '角色管理', name: 'role' }],
        'poster': [{ label: '海报管理', name: 'poster' }],
        'material': [{ label: '素材管理', name: 'material' }],
      };

      this.breadcrumbItems = breadcrumbMap[index] || [];

      if (index === 'home') {
        this.tabActiveName = 'home'; // 激活首页标签页
      } else {
        this.addTab(menuMap[index], index);
      }
    },
    toggleAside() {
      this.isCollapse = !this.isCollapse;
    },
    addTab(label, name) {
      // 检查是否已存在相同名称的标签页，避免重复添加
      if (!this.tabs.find(tab => tab.name === name)) {
        this.tabs.push({ label, name });
        this.tabActiveName = name; // 激活新添加的标签页
      } else {
        this.tabActiveName = name; // 如果标签页已存在，则直接激活
      }
    },
    handleTabClick(tab) {
      this.activeMenu = tab.name; // 更新侧边栏激活项
      this.updateBreadcrumb(tab.name); // 更新面包屑导航
    },
    handleBreadcrumbClick(name) {
      this.tabActiveName = name; // 更新激活的标签页
      this.activeMenu = name; // 更新侧边栏激活项
      this.updateBreadcrumb(name); // 更新面包屑导航
    },
    removeTab(targetName) {
      // 找到要删除的标签页的索引
      const tab = this.tabs.find(tab => tab.name === targetName);
      if (!tab) return;

      let activeName = this.tabActiveName;
      if (activeName === targetName) {
        const index = this.tabs.findIndex(tab => tab.name === targetName);
        const nextTab = this.tabs[index + 1] || this.tabs[index - 1];
        if (nextTab) {
          activeName = nextTab.name;
        }
      }
      this.tabActiveName = activeName;
      this.tabs = this.tabs.filter(tab => tab.name !== targetName);

      this.activeMenu = this.tabActiveName; // 更新侧边栏激活项
      this.updateBreadcrumb(this.tabActiveName); // 更新面包屑导航
    },
    updateBreadcrumb(name) {
      const breadcrumbMap = {
        'home': [],
        'user': [{ label: '系统管理', name: 'system' }, { label: '用户管理', name: 'user' }],
        'menu': [{ label: '系统管理', name: 'system' }, { label: '菜单管理', name: 'menu' }],
        'role': [{ label: '系统管理', name: 'system' }, { label: '角色管理', name: 'role' }],
        'poster': [{ label: '海报管理', name: 'poster' }],
        'material': [{ label: '素材管理', name: 'material' }],
      };

      this.breadcrumbItems = breadcrumbMap[name] || [];
    },
  },
};
</script>

<style scoped>
html,
body,
#app {
  height: 100%;
  margin: 0;
}

.el-container {
  height: 100%;
}

.el-aside {
  background-color: #333;
  color: #fff;
  height: 100%;
}

.avatar_box {
  display: flex;
  align-items: center;
  padding-left: 12px;
  background-color: #333; /* 设置为深色 */
  height: 60px;
  border-bottom: 1px solid #555;
}

.avatar {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  margin-right: 10px;
}

.system_name {
  font-size: 16px;
  font-weight: bold;
  color: #fff;
  transition: opacity 0.3s; /* 添加过渡效果 */
}

.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 16px;
}

.header-left {
  display: flex;
  align-items: center;
}

.header-right {
  display: flex;
  align-items: center;
}

.header-right .el-avatar {
  margin-right: 10px;
}

.el-main {
  background-color: #e9eef3;
  color: #333;
  text-align: center;
  flex-grow: 1;
}

.breadcrumb {
  margin-left: 20px;
  color: #333;
  font-size: 14px;
}

::v-deep .el-icon-arrow-right:before {
  color: black;
}

.breadcrumb .el-breadcrumb__inner {
  font-size: 14px;
}

.el-menu-vertical-demo {
  background-color: #333; /* 设置为深色 */
  border-right: none;
}

.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 200px;
  min-height: 400px;
}

::v-deep .el-menu-item,
::v-deep .el-submenu__title {
  background-color: #333; /* 设置为深色 */
  color: #fff;
}

::v-deep .el-menu-item:hover,
::v-deep .el-submenu__title:hover,
::v-deep .el-menu-item.is-active {
  background-color: #444 !important;
}

::v-deep .el-submenu__icon-arrow {
  display: none;
}
</style>
